import {getActivityList} from "../../api/activity";
import Util from "../../utils/util";

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    parameter: {
      title: "搜索",
      isBack: true,
    },
    search: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  onClear() {
    this.setData({
      search: '',
      lists: [],
      loading: false,
      loadend: false,
      page: 1,
    });
  },

  onInput(e) {
    this.setData({
      search: e.detail.value
    })
  },

  confirmInput(e) {
    this.setData({
      search: e.detail.value,
      page: 1,
      loadend: false,
      lists: [],
    });
    this.getLists();
  },

  getLists() {
    const that = this
    if (that.data.loadend || that.data.loading) {
      return;
    }

    if (Util.isEmpty(this.data.search)) {
      this.setData({
        search: '',
        list: [],
        loading: false,
        loadend: false,
        page: 1,
      })

      return Util.Tips({title: "请输入要搜索的关键词", hideMask: true});
    }

    let filter = that.data.filter || {}
    filter['page_num'] = that.data.page;
    filter['skey'] = that.data.search;
    that.setData({loading: true, loadTitle: '正在加载...'});

    let lists = that.data.lists || []
    getActivityList(filter).then(res => {
      const list = res.data.items || []
      const loadend = res.data.last_page <= that.data.page;
      lists = Util.SplitArray(list, lists);
      /*for (const k in lists) {
        if (lists[k].score <= 0) continue;
        let nums = [];
        for (let i = 0; i < parseInt(lists[k].score || 0); i++) {
          nums.push(i)
        }
        lists[k].score_nums = nums;
      }*/
      that.setData({
        lists: lists,
        loadend: loadend,
        loading: false,
        loadTitle: loadend ? '已全部加载' : '正在加载...',
        total: res.data.total,
        page: that.data.page + 1,
        et_show: lists.length > 0
      });
      wx.stopPullDownRefresh()
    }).catch(errmsg => {
      wx.stopPullDownRefresh()
      app.Tips({title: errmsg})
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})